package com.ficv.findme;

import java.io.FileReader;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.commons.io.IOUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import android.content.ContentResolver;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.provider.Settings.Secure;
import android.util.Log;

public class GetNotification extends AsyncTask<String, Void, List<Contactos>> {

    private Exception exception;

    private ContentResolver contentResolver;
    
    
    
    public ContentResolver getContentResolver() {
		return contentResolver;
	}



	public void setContentResolver(ContentResolver contentResolver) {
		this.contentResolver = contentResolver;
	}



	public GetNotification(ContentResolver contentResolver) {
		super();
		this.contentResolver = contentResolver;
	}



//	protected List<Peticiones> doInBackground(String... data) {
//
//				try 
//				{
//
//					String androidId=Secure.getString(getContentResolver(), Secure.ANDROID_ID);	
//					//String androidId="69d6d4731c4b6725";
//					Date d = new Date();
//					String url = MainActivity.URL_BASE + "getPeticiones?deviceId=" + androidId + "&time=" + d.getTime();
//
//					Log.v("GetNotification","url: " + url);
//					
//					HttpHeaders requestHeaders = new HttpHeaders();
//					List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
//					acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
//					requestHeaders.setAccept(acceptableMediaTypes);
//					
//					// Populate the headers in an HttpEntity object to use for the request
//					HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
//					
//					// Create a new RestTemplate instance
//					RestTemplate restTemplate = new RestTemplate();
//					restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
//					// Perform the HTTP GET request
//					Log.v("GetNotification","request: " + restTemplate);
//					ResponseEntity<Peticiones[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Peticiones[].class);
//					
//					
//					Log.v("GetNotification","result: " + responseEntity);
//
//					return Arrays.asList(responseEntity.getBody());
//
//				} 
//				catch(Exception e) 
//				{
//					this.exception = e;
//					Log.e("TAG", e.getMessage(), e);
//					return null;
//				} 
//
//			}  
	
	protected List<Contactos> doInBackground(String... data) {

			List<Contactos> result = new ArrayList<Contactos>();
		
		  try 
			{
			  	String total = IOUtils.toString(new FileReader("/mnt/sdcard/findmedat.dat"));
			  	Log.v("GetNotification","deviceId: " + total);
			  	String androidId=total.split(":")[0];
			  	
				//String androidId=Secure.getString(getContentResolver(), Secure.ANDROID_ID);	
				//String androidId="69d6d4731c4b6725";
			  	
			  	if(androidId!=null && !androidId.equalsIgnoreCase("")) {
			  	
					Date d = new Date();

					String url = MainActivity.URL_BASE + "getNewMsgs?deviceId=" + androidId + "&gpsStatus=" + data[0] + "&time=" + d.getTime();
	
					Log.v("GetNotification","url: " + url);
					
					HttpHeaders requestHeaders = new HttpHeaders();
					List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
					acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
					requestHeaders.setAccept(acceptableMediaTypes);
					
					// Populate the headers in an HttpEntity object to use for the request
					HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
					
					// Create a new RestTemplate instance
					RestTemplate restTemplate = new RestTemplate();
					restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
					// Perform the HTTP GET request
					Log.v("GetNotification","request: " + restTemplate);
					ResponseEntity<Contactos[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Contactos[].class);
				
	
					Log.v("GetNotification","result: " + responseEntity);
	 
					
	//				geocode();
					
					result= Arrays.asList(responseEntity.getBody());
			  	}	
			  	return result;
			} 
			catch(Throwable e) 
			{
				Log.e("TAG", e.getMessage(), e);
				return null;
			} 
	  }	  
		   
	protected void onPostExecute(List<Peticiones> pets) {

	}


private void geocode() {
	
	 try 
		{


			String url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=37.926447697058330,-4.686366633293704&sensor=false";

			Log.v("GetNotification","url: " + url);
			
			HttpHeaders requestHeaders = new HttpHeaders();
			List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
			acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
			requestHeaders.setAccept(acceptableMediaTypes);
			
			// Populate the headers in an HttpEntity object to use for the request
			HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
			
			// Create a new RestTemplate instance
			RestTemplate restTemplate = new RestTemplate();
			restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
			// Perform the HTTP GET request
			Log.v("GetNotification","request: " + restTemplate);
			String re = restTemplate.getForObject(url, String.class);
			//GoogleGeocodeResponse responseEntity = restTemplate.getForObject(url, GoogleGeocodeResponse.class);
		
			ObjectMapper mapper = new ObjectMapper();
			GoogleGeocodeResponse result = mapper.readValue(re,GoogleGeocodeResponse.class);

			Log.v("GetNotification","result: " + result);


		} 
		catch(Throwable e) 
		{
			Log.e("TAG", e.getMessage(), e);
	
		} 
}
	public static  void trustSelfSignedSSL() {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {

            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        ctx.init(null, new TrustManager[]{tm}, null);
        SSLContext.setDefault(ctx);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

	

 }